package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.BarCodeBean;
import com.scisoft.scan.data.PartBean;
import com.scisoft.scan.data.check.CheckOrderBean;
import com.scisoft.scan.data.depot.DepotOrderDetailBean;
import com.scisoft.scan.data.depot.WarehouseBean;
import com.scisoft.scan.data.pack.PackBoxBean;
import com.scisoft.scan.data.pack.PackDetail04;
import com.scisoft.scan.data.quickcheck.InitCheckScanBean;
import com.scisoft.scan.data.quickcheck.InitCheckStatisticsBean;
import com.scisoft.scan.data.quickcheck.QuickCheckDetailBean;
import com.scisoft.scan.data.quickcheck.QuickCheckStockBean;
import com.scisoft.scan.data.stock.StockInfoBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_QuickCheck {

    public static String GetWS_Url() {
        return String.format("http://%s/dymps/ws/QuickCheckServer?wsdl", SysParameter.WS_IPandPort);
    }

    public static String WS_Method = "receive";

    public static String WS_Namespace = "http://quickCheck.scan.ws.dms.com/";

    //查询配件列表 condition 传入查询条件
    public static List<QuickCheckStockBean> QUICKCHECK_01(WarehouseBean depot,String bizId,String orderId) throws Exception {

        List<QuickCheckStockBean> ret = new ArrayList<>();
        QuickCheckStockBean q = new QuickCheckStockBean();
        q.depotName = "J仓";
        q.partCode = "J001";
        q.partName = "方向盘";
        ret.add(q);
        return ret;
        //以下代码可以复用
        /*
        List<QuickCheckStockBean> ret = new ArrayList<>();
        if(!(bizId==null || "".equals(bizId))){
            bizId=bizId+","+depot.depotId;
        }else{
            bizId="@@,"+depot.depotId;
        }
        if(!(orderId==null || "".equals(orderId))){
            bizId=bizId+","+orderId;
        }else{
            bizId=bizId + ",@@";
        }
        String arg0 = String.format( "{\"flowNo\":\"QUICKCHECK_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<QuickCheckStockBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
        */
    }

    //创建盘点单,返回盘点单的id。输入参数中orderId如果为null，表示盘点单需要创建；如果不为null，表示只更新明细即可。
    public static String QUICKCHECK_02(QuickCheckDetailBean quickCheckDetailBean, WarehouseBean warehouseBean,String orderId) throws Exception {
        String orderNo = "abcde0001";
        return orderNo;
        /*
        Gson gson = new Gson();
        String parm = gson.toJson(quickCheckDetailBean);
        String arg0 = String.format("{\"flowNo\":\"QUICKCHECK_02\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence,orderId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        CheckOrderBean ret=new CheckOrderBean();
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(),new TypeToken<CheckOrderBean>(){}.getType());
        }
        return ret.orderId;
        */
    }

    //获取盘点单列表 bizId：盘点单的编号
    public static List<QuickCheckDetailBean> QUICKCHECK_03(String bizId) throws Exception{

        List<QuickCheckDetailBean> ret = new ArrayList<>();
        QuickCheckDetailBean q = new QuickCheckDetailBean();
        q.depotName = "J仓";
        q.partCode = "J001";
        q.partName = "方向盘";
        ret.add(q);
        return ret;
        //以下代码可以复用
        /*
        List<QuickCheckDetailBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"QUICKCHECK_03\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<QuickCheckDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
        */
    }

    //盘点单审核
    public static String QUICKCHECK_04(String bizId) throws Exception {
        String ret=WebServiceHelper_Check.CHECK_04(bizId);
        return ret;
    }

    //提交唯一码数据
    public static String QUICKCHECK_05(String partCode,String supplierCode,String barCode)  throws Exception {
//        return "成功";
        PartBean ret =new PartBean(partCode,supplierCode,barCode);
        Gson gson = new Gson();
        String parm =gson.toJson(ret);
        String arg0 = String.format( "{\"flowNo\":\"QUICKCHECK_05\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //查询配件盘点统计数据
    public static InitCheckStatisticsBean QUICKCHECK_06(String partCode,String supplierCode ) throws Exception {
//        InitCheckStatisticsBean ret = new InitCheckStatisticsBean();
//        ret.partName = "ABCDEFG";
//        ret.partNo = "P023i2322323";
//        ret.supplierCode = "C2322323";
//        ret.supplierName = "吉林汽车";
//        InitCheckScanBean i1 = new InitCheckScanBean();
//        i1.serialNo = "1111111";
//        i1.supplierCode = "1111111";
//        ret.serialNoList.add(i1);
//        InitCheckScanBean i2 = new InitCheckScanBean();
//        i2.serialNo = "2222222";
//        i2.supplierCode = "2222222";
//        ret.serialNoList.add(i2);
//        InitCheckScanBean i3 = new InitCheckScanBean();
//        i3.serialNo = "3333333";
//        i3.supplierCode = "3333333";
//        ret.serialNoList.add(i3);
//        return ret;
        String bizId= partCode+","+supplierCode;
        String arg0 = String.format( "{\"flowNo\":\"QUICKCHECK_06\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        InitCheckStatisticsBean ret = new InitCheckStatisticsBean();
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");

        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject  jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<InitCheckStatisticsBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //删除提交的数据
    public static String QUICKCHECK_07(String partCode,String supplierCode,String serialNo)  throws Exception {
//        return "成功";
        Gson gson = new Gson();
        InitCheckStatisticsBean bean=new InitCheckStatisticsBean();
        bean.partNo=partCode;
        bean.supplierCode=supplierCode;
        String parm =gson.toJson(bean);
        String arg0 = String.format("{\"flowNo\":\"QUICKCHECK_07\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user,parm, SysParameter.ScSequence,serialNo);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret= "成功";
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    public static SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope, headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
